package com.qf.service.impl;

import com.qf.dao.OrdersMapper;
import com.qf.dao.OrdersRepository;
import com.qf.pojo.req.MyOrders;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.Orders;
import com.qf.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author guoxu
 * @create 2021/3/11 17:19
 */
@Service
public class OrdersServiceImpl implements OrdersService {




    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    OrdersRepository ordersRepository;

    @Override
    public BaseResp findAll(Integer uid) {
        BaseResp baseResp = new BaseResp();
        Map map = new HashMap();
        List all = new ArrayList();
        List<Orders> byUid = ordersRepository.findByUid(uid);
        if (byUid.isEmpty()){
            baseResp.setMessage("您没有订单");
            baseResp.setCode(201);
            return baseResp;
        }
        for (Orders orders : byUid){
            map.put("uid",uid);
            map.put("did",orders.getDid());
            map.put("bid",orders.getBid());
             all = ordersMapper.findAlll(map);

        }
        baseResp.setCode(200);
        baseResp.setData(all);
        return baseResp;

    }

    @Override
    public BaseResp findByUidAndState(Integer uid, String state) {
        //定义返回值
        BaseResp baseResp = new BaseResp();
        //根据id 和状态查询订单集合
        List<Orders> byUidAndState = ordersRepository.findByUidAndState(uid, state);
        //没有
        if (byUidAndState.isEmpty()){
            baseResp.setMessage("您没有该状态的订单");
            baseResp.setCode(201);
            return baseResp;
        }
        //有
        baseResp.setCode(200);
        baseResp.setData(byUidAndState);
        return baseResp;
    }

    @Override
    public BaseResp findById(Map map) {
        //定义返回类型
        BaseResp baseResp = new BaseResp();
        //从redis中查找订单
        Object o = redisTemplate.opsForHash().get(map.get("onumber").toString(), map.get("id").toString());
        //如果redis没有，证明没有开始配送,则从数据库查找,直接返回
        if (StringUtils.isEmpty(o)){
            Optional<Orders> id = ordersRepository.findById(Integer.valueOf(map.get("id").toString()));
            //mysql订单不存在
            if(StringUtils.isEmpty(id)) {
                baseResp.setMessage("该订单走丢了");
                baseResp.setCode(201);
                return baseResp;
            }
            //mysql订单存在
            baseResp.setData(id.get());
            baseResp.setCode(200);
            return baseResp;
        }
        //如果redis的订单存在
        baseResp.setData(o);
        baseResp.setCode(200);

        return baseResp;
    }

    @Override
    public BaseResp distribution(Map map) {
        //定义返回类型
        BaseResp baseResp = new BaseResp();
        //修改订单状态为配送中
        int id = ordersMapper.updateState(Integer.valueOf(map.get("id").toString()));
        //修改成功
        if (id==1){
            //查出来修改后的订单
            Optional<Orders> byId = ordersRepository.findById(Integer.valueOf(map.get("id").toString()));
            //存入redis中
            redisTemplate.opsForHash().put(map.get("onumber").toString(),map.get("id").toString(),byId.get());
            //返回到前端
            baseResp.setCode(200);
            baseResp.setMessage("开始配送");
            return baseResp;
        }
        //修改失败，并返回
        baseResp.setMessage("开始配送失败");
        baseResp.setCode(201);
        return baseResp;
    }

    @Override
    public BaseResp add(Orders orders){
        //定义返回类型
        BaseResp baseResp = new BaseResp();
        //生成新的订单
        orders.setState("WAIT_PAY");
        long  l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        orders.setOTime(parse);
        UUID uuid = UUID.randomUUID();
        orders.setOnumber(uuid.toString());
        orders.setEvaluation("未评价");
        Orders save = ordersRepository.save(orders);
        baseResp.setCode(200);
        baseResp.setMessage("恭喜您下单成功");
        return baseResp;
    }

    @Override
    public BaseResp sign(Orders orders) throws ParseException {
        //定义返回类型
        BaseResp baseResp = new BaseResp();
        //签收
        long  l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = sdf.parse(sdf.format(date));
        //修改签收时间，订单状态，地址
        orders.setUpdateTime(parse);
        orders.setState("已签收");

        orders.setOrigin(orders.getDestination());
        ordersMapper.updateSign(orders);
        //从redis中删除
        redisTemplate.opsForHash().delete(orders.getOnumber(),orders.getId().toString());
        //返回
        baseResp.setCode(200);
        baseResp.setMessage("已签收成功，请您享用");
        return baseResp;
    }

    @Override
    public BaseResp update(Orders orders) {
        BaseResp baseResp = new BaseResp();
        ordersRepository.saveAndFlush(orders);
        baseResp.setCode(200);
        baseResp.setMessage("支付状态修改成功");
        return baseResp;
    }

    @Override
    public BaseResp findByOnumber(String oNumber) {
        BaseResp baseResp = new BaseResp();
        Orders orders = ordersRepository.findByOnumber(oNumber);
        baseResp.setCode(200);
        baseResp.setData(orders);
        baseResp.setMessage("查询成功");
        return baseResp;
    }

    @Override
    public BaseResp findByBid(Integer bid) {
        BaseResp baseResp = new BaseResp();
        List<Orders> byUid = ordersRepository.findByBid(bid);
        if (byUid.isEmpty()){
            baseResp.setMessage("您没有订单");
            baseResp.setCode(201);
            return baseResp;
        }
        baseResp.setCode(200);
        baseResp.setData(byUid);
        return baseResp;
    }

    @Override
    public BaseResp findByBidAndState(Integer bid, String state) {
        //定义返回值
        BaseResp baseResp = new BaseResp();
        //根据id 和状态查询订单集合
        List<Orders> byBidAndState = ordersRepository.findByBidAndState(bid, state);
        //没有
        if (byBidAndState.isEmpty()){
            baseResp.setMessage("您没有该状态的订单");
            baseResp.setCode(201);
            return baseResp;
        }
        //有
        baseResp.setCode(200);
        baseResp.setData(byBidAndState);
        return baseResp;
    }

    @Override
    public BaseResp updateEvaluation(Orders orders) {
        BaseResp baseResp = new BaseResp();
        ordersMapper.updateEvaluation(orders);
        List o = (List) redisTemplate.opsForHash().get(orders.getBName(), orders.getBid().toString());
        if (o==null||o.isEmpty()){
            List<String> list = new ArrayList<>();
            list.add(orders.getEvaluation());
            redisTemplate.opsForHash().put(orders.getBName(),orders.getBid().toString(),list);
        }

        o.add(orders.getEvaluation());
        redisTemplate.opsForHash().put(orders.getBName(),orders.getBid().toString(),o);
        baseResp.setCode(200);
        baseResp.setMessage("评价成功,即将跳转至未评价页面");
        return baseResp;
    }

    @Override
    public BaseResp findByUidAndEvaluation(Integer uid, String evaluation,String state) {
        BaseResp baseResp = new BaseResp();

        List<Orders> byUidAndEvaluation = ordersRepository.findByUidAndEvaluationAndState(uid, evaluation, state);
        //没有
        if (byUidAndEvaluation.isEmpty()){
            baseResp.setMessage("您没有该状态的订单");
            baseResp.setCode(201);
            return baseResp;
        }
        //有
        baseResp.setCode(200);
        baseResp.setData(byUidAndEvaluation);
        return baseResp;
    }

    @Override
    public BaseResp findByBidAndNum(String bid, String bname) {
        Set<String> set = new HashSet<>();
        BaseResp baseResp = new BaseResp();
        List o = (List) redisTemplate.opsForHash().get(bname, bid);
        if (o.isEmpty()){
            baseResp.setMessage("您还没有评价");
            baseResp.setCode(200);
        }
        for (int i=0;i<10;i++){
            set.add(o.get(i).toString());
        }
        baseResp.setData(set);
        baseResp.setCode(200);
        return baseResp;
    }
}
